package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.db.DBConn;
import dao.ReplyMagDao;
import domain.Reply;

/**
 * 
 * May 19, 2009
 *
 * @author Fabian
 *
 */
public class ReplyMagImpl implements ReplyMagDao{

	/* (non-Javadoc)
	 * @see dao.ReplyMagDao#addReply(vo.Reply)
	 */
	
	public boolean addReply(Reply r) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "insert into t_topic_reply values (null, ?, ?, now(), ?, ?)";
		//更新回复数
		String sql2 = "update t_topic set reply_count = reply_count + 1 where id = ?";
		try {
			conn = DBConn.getConn();
			//关闭自动提交
			conn.setAutoCommit(false);			
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, r.getTopic_id());
			pstmt.setString(2, r.getContent());
			pstmt.setInt(3, r.getUser_id());
			pstmt.setInt(4, r.getRole_id());			
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				pstmt = DBConn.getPstmt(conn, sql2);
				pstmt.setInt(1, r.getTopic_id());
				int rsColum2 = pstmt.executeUpdate();
				if (rsColum2 > 0) {
					result = true;
					return result;
				} else {
					//回滚
					conn.rollback();
				}
			} else {
				//回滚
				conn.rollback();
			}
		} catch(SQLException e) {
			try {
				//回滚
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			return result;
		} finally {
			try {
				//恢复自动提交
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.ReplyMagDao#delReply(int)
	 */
	
	public boolean delReply(int id) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		//查询
		String sql1 = "select topic_id from t_topic_reply where id = ?";
		//删除
		String sql2 = "delete from t_topic_reply where id = ?";		
		//更新回复数
		String sql3 = "update t_topic set reply_count = (reply_count - 1) where id = ?";
		try {
			conn = DBConn.getConn();	
			//关闭自动提交
			conn.setAutoCommit(false);	
			pstmt = DBConn.getPstmt(conn, sql1);			
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				int topic_id = rs.getInt(1);
				pstmt = DBConn.getPstmt(conn, sql2);
				pstmt.setInt(1, id);
				int rsColum = pstmt.executeUpdate();
				if (rsColum > 0) {
					pstmt = DBConn.getPstmt(conn, sql3);
					pstmt.setInt(1, topic_id);
					int rsColum2 = pstmt.executeUpdate();
					if (rsColum2 > 0) {
						conn.commit();
						result = true;
						return result;	
					} else {
						//回滚
						conn.rollback();
					}
				} else {
					//回滚
					conn.rollback();
				}
			}
		} catch(SQLException e) {
			try {
				//回滚
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			return result;
		} finally {
			try {
				//恢复自动提交
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.ReplyMagDao#getPageReply(int, int, int)
	 */
	
	public List<Reply> getPageReply(int start, int limit, int topic_id) {
		List<Reply> list = new ArrayList<Reply>();		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String sql = "select * from t_topic_reply where topic_id = ? order by create_time desc limit ?, ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, topic_id);
			pstmt.setInt(2, start);
			pstmt.setInt(3, limit);
			rs = pstmt.executeQuery();			
			while (rs.next()) {
				Reply r = new Reply();
				r.setId(rs.getInt("id"));				
				r.setContent(rs.getString("content"));
				int user_id = rs.getInt("author_id");
				int roleId = rs.getInt("role_id");				
				r.setPubDate(rs.getTimestamp("create_time"));
								
				if (roleId == 3) {		//如果是学生
					r.setRole("学生");
					String sql3 = "select name from t_student where id = ?"; 
					pstmt = DBConn.getPstmt(conn, sql3);
					pstmt.setInt(1, user_id);
					rs2 = pstmt.executeQuery();	
					if (rs2.next()) {
						r.setAuthor(rs2.getString(1));
					}
					rs2.close();
				} else if (roleId == 2) {			//如果是老师
					r.setRole("老师");
					String sql2 = "select name from t_teacher where id = ?"; 
					pstmt = DBConn.getPstmt(conn, sql2);
					pstmt.setInt(1, user_id);
					rs2 = pstmt.executeQuery();	
					if (rs2.next()) {
						r.setAuthor(rs2.getString(1));
					}
					rs2.close();
				} else if (roleId == 1) {			//如果是管理员
					r.setRole("管理员");
					String sql1 = "select name from t_admin where id = ?"; 
					pstmt = DBConn.getPstmt(conn, sql1);
					pstmt.setInt(1, user_id);
					rs2 = pstmt.executeQuery();	
					if (rs2.next()) {
						r.setAuthor(rs2.getString(1));
					}
					rs2.close();
				}	
				list.add(r);
			}
			return list;
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}		
	}

	/* (non-Javadoc)
	 * @see dao.ReplyMagDao#getReplyTotalCount(int)
	 */
	
	public int getReplyTotalCount(int topic_id) {
		int totalCount = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select count(*) from t_topic_reply where topic_id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, topic_id);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				totalCount = rs.getInt(1);
				return totalCount;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return totalCount;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		return 0;
	}
}
